package com.controller;

import com.bean.DateInfo;
import com.bean.ShoppingInfo;
import com.bean.other.MetaInfo;
import com.bean.other.RequestData;
import com.bean.other.ResponseData;
import com.github.pagehelper.PageInfo;
import com.service.impl.DateInfoService;
import com.service.impl.ShoppingInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("api/shopping_info")
public class ShoppingInfoController {

    @Autowired
    private ShoppingInfoService shoppingInfoService;

    @PostMapping("add")
    public ResponseEntity<ResponseData> add(@RequestBody ShoppingInfo shoppingInfo){
        shoppingInfoService.insert(shoppingInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
        //return  ResponseEntity.status(HttpStatus.NOT_FOUND).body("没有找到");//直接设置响应头
    }

    @PostMapping("edit")
    public ResponseEntity<ResponseData> edit(@RequestBody ShoppingInfo shoppingInfo){
        shoppingInfoService.update(shoppingInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @DeleteMapping("del/{id}")
    public ResponseEntity<ResponseData> del(@PathVariable String id){
        ShoppingInfo shoppingInfo=new ShoppingInfo();
        shoppingInfo.setId(id);
        shoppingInfoService.delete(shoppingInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @GetMapping("get")
    public ResponseEntity<ResponseData> get(@RequestParam("id")String id){
        ShoppingInfo shoppingInfo=shoppingInfoService.get(id);
        return ResponseEntity.ok(ResponseData.success(shoppingInfo));
    }

    @PostMapping("list")//@RequestBody只能使用一次，使用的是流，用过后就不能用了。
    public ResponseEntity<ResponseData> list(@RequestBody RequestData<ShoppingInfo> requestData){
        ShoppingInfo shoppingInfo=requestData.getObj();
        shoppingInfo.setCreator(requestData.getUserid());
        MetaInfo metaInfo =requestData.getMetaInfo();
        PageInfo<ShoppingInfo> pageInfo=shoppingInfoService.select(shoppingInfo,metaInfo);
        metaInfo.setPages(pageInfo.getPages());
        metaInfo.setTotal((int) pageInfo.getTotal());
        return ResponseEntity.ok(ResponseData.success(pageInfo.getList(),metaInfo));
    }
}
